// pages/member/IntegralGetRecord/IntegralGetRecord.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integral: '积分',
    statusValue: 0,
    selectShow: false,
    selectType: 0,
    record_list: [],
    money_arr: [],
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    show: false,
    postDate: "",
    formatter(type, val) {
      if (type === 'year') {
        return `${val}年`;
      } else if (type === 'month') {
        return `${val}月`;
      }
      return val;
    },


    total_page: 0,
    page: 1,
    isLoadMore: true,
    loading: false, //为true时不会触发加载更多，false可以触发
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    try {
      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
        // Do something with return value
        if (this.data.integral) {
          wx.setNavigationBarTitle({
            title: this.data.integral + '列表'
          });
        }
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  showTap(){
    this.setData({
      show:false
    })
  },
  cancel(){
    this.setData({
      show:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData(this.data.page);
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.point-activity.Frontend.Modules.Activity.Controllers.receive-records.index");
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        page_id: 1
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let activityDate = res.data;
          that.setData({
            record_list: activityDate.record_list,
            total_page: activityDate.record_list.last_page ? activityDate.record_list.last_page : 0,
            noneShow:false,
            money_arr:activityDate.point_arr
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }

      },
      fail: function (res) {}
    });
  },
  _getMoreData() {
    let that = this;
    if (that.data.page >= that.data.total_page) {
      return;
    } else {
      that.setData({
        page: that.data.page + 1
      });
      let urlStr = app.getNetAddresss(
        "plugin.point-activity.Frontend.Modules.Activity.Controllers.receive-records.index");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page_id: that.data.page
        },
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            var nextPageData = res.data.record_list.data;
            for (const key in nextPageData) {
              if (this.data.record_list.data[key]) {
                this.data.record_list.data[key] = [...this.data.record_list.data[key], ...nextPageData[key]]
              } else {
                this.data.record_list.data[key] = nextPageData[key]
              }
            }
            that.setData({
              loading: false,
              record_list: this.data.record_list,
              money_arr: Object.assign(this.data.money_arr, res.data.point_arr)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              isLoadMore: false
            });
            return;
          }

        },
        fail: function (res) {}
      });
    }
  },
});